#include <windows.h>
#include "MYSQL\mysql.h"

#include "CDb.h"
#include "CConsole.h"
#include "CSemaphore.h"
#include "Utils/T2AVLList.h"
#include "CGuildShop.h"
#include "CGuildShopHandler.h"

T2List<unsigned short,CGuildShop*>* CGuildShopHandler::m_guildShopList=NULL;

void CGuildShopHandler::checkGuildShopList() {
	if (m_guildShopList==NULL) {
		// Create List
		m_guildShopList = new T3ListProtected<unsigned short,CGuildShop*,T2AVLList>();
	}
}

CGuildShop * CGuildShopHandler::getGuildShop(unsigned short guildID) {
	checkGuildShopList();
	return m_guildShopList->find(guildID);
}

void CGuildShopHandler::addGuildShop(CGuildShop * guildShop) {
	CConsole::outDebOnly("Adding guild shop %i to the guild shop list",guildShop->getId());
	checkGuildShopList();
	m_guildShopList->pushBack(guildShop->getId(),guildShop);
}

void CGuildShopHandler::waitGuildShopList() {
	checkGuildShopList();
	m_guildShopList->unlock();
}

void CGuildShopHandler::exitGuildShopList() {
	checkGuildShopList();
	m_guildShopList->lock();
}

void CGuildShopHandler::initialize() {
	MYSQL_RES * res;
	MYSQL_ROW row;
	char query[8192];
	sprintf(query, "SELECT DISTINCT `guildID` FROM guild_shops");
	int count = (int)strlen(query);

	MYSQL* mysqlConn=CDb::getConn(); //get conn
	mysql_real_query(mysqlConn, query, count);
	res = mysql_store_result(mysqlConn);
	CDb::pushConn(mysqlConn); //release conn

	if(!res) 
	{
		CConsole::outDebOnly("Error looking up guild shops.");
	} 
	else if(mysql_num_rows(res))
	{
		waitGuildShopList();
		while(row = mysql_fetch_row(res)) 
		{
			unsigned short guildID = (unsigned short)atoi(row[0]);
			CGuildShop * guildShop = new CGuildShop(guildID);
			addGuildShop(guildShop);
			guildShop->restock();
		}
		exitGuildShopList();
		mysql_free_result(res);
	}
}

void CGuildShopHandler::UpdateGuildShops() 
{
	waitGuildShopList();
	for (unsigned int guildShopNr = 0; guildShopNr < m_guildShopList->getSize(); guildShopNr++)
	{
		CGuildShop * guildShop = m_guildShopList->at(guildShopNr);
		if (guildShop != NULL) 
		{
			guildShop->updatePrices();
			guildShop->restock();
		}
	}
	exitGuildShopList();
}

unsigned char CGuildShopHandler::removeItemQuantity(unsigned short guildID, unsigned short itemID, unsigned char quantity) {
	CGuildShop * guildShop = NULL;
	guildShop = getGuildShop(guildID);
	if(guildShop != NULL) {
		unsigned char itemQty = 0;
		itemQty = (unsigned char)guildShop->getItemQuantity(itemID);
		if(itemQty >= quantity) {
			guildShop->removeQuantity(itemID, quantity);
			itemQty -= quantity;
			return itemQty;
		} else {
			CConsole::outDebOnly("Not enough quantity to remove from guild shop.");
			return itemQty;
		}
	} else {
		CConsole::outDebOnly("Guild shop %i not found", guildID);
	}
	return 0xFF;
}

unsigned char CGuildShopHandler::addItemQuantity(unsigned short guildID, unsigned short itemID, unsigned char quantity) {
	CGuildShop * guildShop = NULL;
	guildShop = getGuildShop(guildID);
	if(guildShop != NULL) {
		unsigned char itemQty = 0;
		itemQty = (unsigned char)guildShop->getItemQuantity(itemID);
		unsigned char max = 0;
		max = guildShop->getMaxQuantity(itemID);
		if( (itemQty+quantity) <= max) {
			guildShop->addQuantity(itemID, quantity);
			itemQty += quantity;
			return itemQty;
		} else {
			CConsole::outDebOnly("Too much quantity to add to guild shop.");
			return itemQty;
		}
	} else {
		CConsole::outDebOnly("Guild shop %i not found", guildID);
	}
	return 0xFF;
}

unsigned char CGuildShopHandler::getItemQuantity(unsigned short guildID, unsigned short itemID) {
	CGuildShop * guildShop = NULL;
	guildShop = getGuildShop(guildID);
	if(guildShop != NULL) {
		unsigned char itemQty = 0;
		itemQty = (unsigned char)guildShop->getItemQuantity(itemID);
		return itemQty;
	} else {
		CConsole::outDebOnly("Guild shop %i not found", guildID);
	}
	return 0xFF;
}

unsigned long CGuildShopHandler::getItemPrice(unsigned short guildID, unsigned short itemID) {
	CGuildShop * guildShop = NULL;
	guildShop = getGuildShop(guildID);
	if(guildShop != NULL) {
		unsigned long price = 0;
		price = guildShop->getPrice(itemID);
		return price;
	} else {
		CConsole::outDebOnly("Guild shop %i not found", guildID);
	}
	return 0;
}

unsigned long CGuildShopHandler::getItemSellPrice(unsigned short guildID, unsigned short itemID) {
	CGuildShop * guildShop = NULL;
	guildShop = getGuildShop(guildID);
	if(guildShop != NULL) {
		unsigned long price = 0;
		price = guildShop->getSellPrice(itemID);
		return price;
	} else {
		CConsole::outDebOnly("Guild shop %i not found", guildID);
	}
	return 0;
}